package com.cars.util.ip;

import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/7/14 23:16
 */
public class IPUtil {

    /**
     * 判断ip是否在白名单内
     *
     * @param ipList    ip列表
     * @param requestIp 请求ip
     * @return 是否在白名单内
     */
    public static boolean isValidIp(String requestIp, List<String> ipList) {
        if (ipList == null) {
            return false;
        }
        // 特殊IP设置，如果ip列表包含0.0.0.0，则不卡控ip
        if (ipList.contains(requestIp) || ipList.contains("0.0.0.0")) {
            return true;
        }
        ipList = ipList.stream().filter(ip -> ip.contains("~")).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(ipList)) {
            String regex = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}~(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])";
            for (String ip : ipList) {
                if (Pattern.matches(regex, ip)) {
                    String[] ipDetails = ip.split("\\.");
                    String[] ipEnds = ipDetails[3].split("~");
                    String[] ipStart = new String[4];
                    String[] ipEnd = new String[4];
                    for (int i = 0; i < 3; i++) {
                        ipStart[i] = ipDetails[i];
                        ipEnd[i] = ipDetails[i];
                    }
                    ipStart[3] = ipEnds[0];
                    ipEnd[3] = ipEnds[1];
                    String[] ipReq = requestIp.split("\\.");
                    long ips = 0L, ipe = 0L, ipr = 0L;
                    for (int i = 0; i < 4; ++i) {
                        ips = ips << 8 | Integer.parseInt(ipStart[i]);
                        ipe = ipe << 8 | Integer.parseInt(ipEnd[i]);
                        ipr = ipr << 8 | Integer.parseInt(ipReq[i]);
                    }
                    if (ips <= ipr && ipr <= ipe) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
//        System.out.println(IPUtil.ipIsValid("192.168.1.4", "192.168.1.1-192.168.1.10"));
    }
}
